#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "led_strip.h"
#include "sdkconfig.h"
#include "esp_random.h"
#include "ws2812B.h"
#include "app_RGB.h"
#include "app_LCD.h" 
#include "lv_port.h"
#include "lvgl.h"
#include "lv_demos.h"
#include "st7789.h"
#include "app_KEY.h"



void lvgl_task(void *arg)
{
    while(true)
    {
        vTaskDelay(pdMS_TO_TICKS(10));
        lv_task_handler();
    }
}


void GUI_task(void *arg)
{
    lv_obj_t* label;

    static lv_obj_t* default_src;
    default_src = lv_scr_act();                 //获取默认屏幕
    label = lv_label_create(default_src);       //添加一个标签
    lv_label_set_text(label, "hello world_is_red!");   
    lv_obj_center(label);    

    lv_style_t style;
    lv_style_init(&style);
    lv_style_set_text_color(&style,lv_palette_main(LV_PALETTE_RED));
    lv_obj_add_style(label,&style,0);

    //lv_obj_align(label,LV_ALIGN_TOP_MID,30,0);
    while(true)
    {
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

void color_fill_task(void *arg)
{
     LV_UNUSED(arg);

    /* 创建一个覆盖全屏的对象作为背景 */
    lv_obj_t *bg = lv_obj_create(lv_scr_act());
    lv_obj_set_size(bg, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL));
    lv_obj_set_pos(bg, 0, 0);
    lv_obj_clear_flag(bg, LV_OBJ_FLAG_SCROLLABLE); /* 禁止滚动等行为 */

    /* 确保背景完全不透明以便观测颜色 */
    lv_obj_set_style_bg_opa(bg, LV_OPA_COVER, 0);

    /* 颜色序列（你可以任意修改） */
    lv_color_t colors[] = {
        lv_color_hex(0xFF0000), // 红色
        lv_color_hex(0x00FF00), // 绿色
        lv_color_hex(0x0000FF), // 蓝色
        lv_color_hex(0xFFFFFF), // 白色
        lv_color_hex(0x000000), // 黑色
        lv_color_hex(0xFFFF00), // 黄色
        lv_color_hex(0x00FFFF), // 青色
        lv_color_hex(0xFF00FF)  // 品红
    };
    const size_t n_colors = sizeof(colors) / sizeof(colors[0]);
    size_t idx = 0;

    while (1) {
        lv_color_t c = colors[idx];
        lv_obj_set_style_bg_color(bg, c, 0);
        /* LVGL 会在你的 lv_task_handler() 中刷新显示，保持短延迟以便观察 */
        vTaskDelay(pdMS_TO_TICKS(1500));

        idx++;
        if (idx >= n_colors) idx = 0;
    }

    vTaskDelete(NULL);
}

//LV_IMG_DECLARE(one_1);
LV_IMG_DECLARE(HTAO1);
LV_IMG_DECLARE(HTAO2);
LV_IMG_DECLARE(HTAO3);
LV_IMG_DECLARE(HTAO4);
LV_IMG_DECLARE(HTAO5);
LV_IMG_DECLARE(HTAO6);
LV_IMG_DECLARE(HTAO7);
LV_IMG_DECLARE(HTAO8);
LV_IMG_DECLARE(HTAO9);
LV_IMG_DECLARE(HTAO10);
LV_IMG_DECLARE(HTAO11);

void image_task(void *arg)
{
   lv_obj_t* img_tesr_1 = lv_img_create(lv_scr_act());
    while(1)
    {
        lv_img_set_src(img_tesr_1,&HTAO1);
        vTaskDelay(pdMS_TO_TICKS(80));
        lv_img_set_src(img_tesr_1,&HTAO2);
        vTaskDelay(pdMS_TO_TICKS(80));
        lv_img_set_src(img_tesr_1,&HTAO3);
        vTaskDelay(pdMS_TO_TICKS(80));
        lv_img_set_src(img_tesr_1,&HTAO4);
        vTaskDelay(pdMS_TO_TICKS(80));
        lv_img_set_src(img_tesr_1,&HTAO5);
        vTaskDelay(pdMS_TO_TICKS(80));
        lv_img_set_src(img_tesr_1,&HTAO6);
        vTaskDelay(pdMS_TO_TICKS(80));
        lv_img_set_src(img_tesr_1,&HTAO7);
        vTaskDelay(pdMS_TO_TICKS(80));
        lv_img_set_src(img_tesr_1,&HTAO8);
        vTaskDelay(pdMS_TO_TICKS(80));
        lv_img_set_src(img_tesr_1,&HTAO9);
        vTaskDelay(pdMS_TO_TICKS(80));
        lv_img_set_src(img_tesr_1,&HTAO10);
        vTaskDelay(pdMS_TO_TICKS(80));
        lv_img_set_src(img_tesr_1,&HTAO11);
        vTaskDelay(pdMS_TO_TICKS(80));
        //vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

void lvgl_animation_task(void *arg)
{


    while(true)
    {
        lv_timer_handler(); //处理LVGL定时器任务
        vTaskDelay(pdMS_TO_TICKS(10));
    }
}


void app_main(void)
{
    configure_led();//配置RGB灯珠

    lv_port_init();                 //初始化LVGL
    
    st7789_lcd_backlight(true);         //打开背光


    xTaskCreatePinnedToCore(lvgl_task,"lvgl_task",4096,NULL,2,NULL,APP_CPU_NUM);
    //xTaskCreatePinnedToCore(GUI_task,"GUI_task",4096,NULL,3,NULL,APP_CPU_NUM);
    //xTaskCreatePinnedToCore(color_fill_task,"color_fill_task",4096,NULL,4,NULL,APP_CPU_NUM);
    xTaskCreatePinnedToCore(image_task,"image_task",4096,NULL,4,NULL,APP_CPU_NUM);
    xTaskCreatePinnedToCore(app_key_task,"app_key_task",4096,NULL,4,NULL,APP_CPU_NUM);

}

